package com.crm.miaohe.service;

import com.crm.miaohe.common.ApplicationConstant;
import com.crm.miaohe.entity.order.statistics.DailyOrderSummary;
import com.crm.miaohe.entity.order.statistics.HistoryOrderSummary;
import com.crm.miaohe.entity.order.statistics.OrderCustomerSummary;
import com.crm.miaohe.exception.ErpBusinessException;
import com.crm.miaohe.mapper.TblOrderApiMapper;
import com.crm.miaohe.model.TblOrderExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderStatisticsServiceImpl implements OrderStatisticsService {

    private final TblOrderApiMapper orderApiMapper;

    public OrderStatisticsServiceImpl(TblOrderApiMapper orderApiMapper) {
        this.orderApiMapper = orderApiMapper;
    }

    @Override
    public DailyOrderSummary getDailyStatistics() {
        TblOrderExample example = new TblOrderExample();
        example.createCriteria().andCreateTimeGreaterThanOrEqualTo(LocalDate.now().atStartOfDay())
                .andCreateTimeLessThan(LocalDate.now().plusDays(1).atStartOfDay());
        example.createCriteria().andStatusEqualTo(1);
        List<OrderCustomerSummary> summaryList = orderApiMapper.selectOrderSummaryByExample(example);
        if (!ObjectUtils.isEmpty(summaryList)) {
            Integer customerCount = summaryList.size();
            Integer orderCount = summaryList.stream().mapToInt(OrderCustomerSummary::getOrderCount).sum();
            Double totalAmount = summaryList.stream().mapToDouble(OrderCustomerSummary::getAmount).sum();
            DailyOrderSummary summary = DailyOrderSummary.builder().summary(summaryList).build();
            summary.setOrderCount(orderCount);
            summary.setCustomerCount(customerCount);
            summary.setTotalAmount(totalAmount);
            return summary;
        } else {
            throw new ErpBusinessException(HttpStatus.NOT_FOUND,
                    ApplicationConstant.DATA_NOT_FOUND, "当日订单", LocalDate.now());
        }
    }

    @Override
    public HistoryOrderSummary getHistorySummary(@NonNull Date start, @NonNull Date end) {
        TblOrderExample example = new TblOrderExample();
        example.createCriteria().andCreateTimeGreaterThanOrEqualTo(
                LocalDate.ofInstant(start.toInstant(), ZoneId.systemDefault()).atStartOfDay()
        ).andCreateTimeLessThan(
                LocalDate.ofInstant(end.toInstant(), ZoneId.systemDefault()).plusDays(1).atStartOfDay()
        );
        example.createCriteria().andStatusEqualTo(1);
        List<OrderCustomerSummary> summaryList = orderApiMapper.selectOrderSummaryByExample(example);
        if (!ObjectUtils.isEmpty(summaryList)) {
            HistoryOrderSummary summary = new HistoryOrderSummary();
            summary.setCustomerCount(summaryList.size());
            Integer orderCount = summaryList.stream().mapToInt(OrderCustomerSummary::getOrderCount).sum();
            summary.setOrderCount(orderCount);
            Double totalAmount = summaryList.stream().mapToDouble(OrderCustomerSummary::getAmount).sum();
            summary.setTotalAmount(totalAmount);
            return summary;
        } else {
            throw new ErpBusinessException(HttpStatus.NOT_FOUND,
                    ApplicationConstant.DATA_NOT_FOUND, "当前时间段订单", start.toString() + "-" + end.toString());
        }
    }
}
